package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.exception.BusinessException;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.utils.JwtUtils;
import com.qk.vo.LoginResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceimpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult<User> listUsers(UserDto userDto) {
        PageHelper.startPage(userDto.getPage(),userDto.getPageSize());
        List<User> userList = userMapper.listAll(userDto);
        Page<User> page = (Page<User>) userList;
        return new PageResult<>(page.getTotal(),page.getResult());
    }

/*    @Override
    public void deleleByid(Integer id) {
        userMapper.deleleByid(id);
    }*/

    @Override
    public void insert(User user) {
        String password = user.getUsername()+"123";
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userMapper.insert(user);
    }

    @Override
    public User getById(Integer id) {
        User user = userMapper.getById(id);
        return user;
    }

    @Override
    public void update(User user) {
        user.setPassword(DigestUtils.md5DigestAsHex((user.getUsername()+"123").getBytes()));
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public List<User> getUser() {
        List<User> userList = userMapper.getUser();
        return userList;
    }

    @Override
    public List<User> listByRole(String roleLabel) {
        List<User> userList = userMapper.listByRole(roleLabel);
        return userList;
    }

    @Override
    public List<User> listByDept(Integer deptId) {
        List<User> userList = userMapper.listByDept(deptId);
        return userList;
    }

    @Override
    public void deletes(List<Integer> ids) {
        userMapper.deletes(ids);
    }
    @Override
    public LoginResultVo selectbyusername(User user) {
        User u=userMapper.selectbyusername(user.getUsername());
        if( u == null)
        {
            throw new BusinessException("用户名或密码错误");
        }

        String message=DigestUtils.md5DigestAsHex((user.getPassword()).getBytes());
        if(! message.equals(u.getPassword()))
        {
            throw new BusinessException("用户名或密码错误");
        }
        if(u.getStatus()!=1)
        {
            throw new BusinessException("用户被禁用");
        }
        LoginResultVo loginResultVo=new LoginResultVo();
        loginResultVo.setId(u.getId());
        loginResultVo.setUsername(u.getUsername());
        loginResultVo.setName(u.getName());
        loginResultVo.setImage(u.getImage());
        loginResultVo.setRoleLabel(u.getRoleLabel());

        Map<String,Object> claims = new HashMap<>();
        claims.put("id",u.getId());
        claims.put("username",u.getUsername());
        String jwt =JwtUtils.generateToken(claims);
        loginResultVo.setToken(jwt);
        return loginResultVo;
    }
}
